package com.chenyue.cm.news.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chenyue.cm.base.dao.LcBaseMapper;
import com.chenyue.cm.base.service.BaseServiceImpl;
import com.chenyue.cm.common.vo.Page;
import com.chenyue.cm.news.dao.JdNewsCommentMapper;
import com.chenyue.cm.news.domain.JdNewsComment;
import com.chenyue.cm.news.service.JdNewsCommentService;
import com.chenyue.cm.news.vo.UserNewsComment;
import com.chenyue.cm.user.dao.JdAppUserMapper;
import com.chenyue.cm.user.domain.JdAppUser;
import com.chenyue.cm.utils.DateUtils;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


/**
 * @author chenyue-cm
 */
@Service(value = "JdNewsCommentService")
public class JdNewsCommentServiceImpl extends BaseServiceImpl<JdNewsComment> implements JdNewsCommentService {

    private final JdNewsCommentMapper jdNewsCommentMapper;
    private final JdAppUserMapper jdAppUserMapper;

    public JdNewsCommentServiceImpl(
            @Qualifier("jdNewsCommentMapper") JdNewsCommentMapper jdNewsCommentMapper,
            @Qualifier("jdAppUserMapper") JdAppUserMapper jdAppUserMapper
    ) {
        this.jdNewsCommentMapper = jdNewsCommentMapper;
        this.jdAppUserMapper = jdAppUserMapper;
    }

    @Override
    @Resource(name = "jdNewsCommentMapper")
    public void setBaseMapper(LcBaseMapper<JdNewsComment, Serializable> baseMapper) {
        this.baseMapper = baseMapper;
    }

    @Override
    public Page<List<UserNewsComment>> getNewsComments(Integer page, Integer rows, Integer newsId) {
        //返回列表
        List<UserNewsComment> userNewsCommentList = new ArrayList<>();

        LambdaQueryWrapper<JdNewsComment> wrapper = new LambdaQueryWrapper<JdNewsComment>()
                .eq(JdNewsComment::getNewsId, newsId);
        Integer count = jdNewsCommentMapper.selectCount(wrapper);
        Page<List<UserNewsComment>> result = new Page<>(page, rows, count);
        wrapper.orderByDesc(JdNewsComment::getCt);
        List<JdNewsComment> jdNewsComments = jdNewsCommentMapper.selectList(wrapper.last(result.convertPage()));
        for (JdNewsComment jdNewsComment : jdNewsComments) {
            UserNewsComment comment = new UserNewsComment();
            comment.setCommentId(jdNewsComment.getId());
            JdAppUser appUser = jdAppUserMapper.selectById(jdNewsComment.getUserId());
            comment.setUserName(appUser.getUserName());
            comment.setHeadImg(appUser.getHeadImg());
            comment.setContent(jdNewsComment.getContent());
            userNewsCommentList.add(comment);
        }
        result.setContent(userNewsCommentList);
        return result;
    }

    @Override
    public int postComment(Integer newsId, Integer userId, String content) {
        JdNewsComment comment = new JdNewsComment();
        comment.setNewsId(newsId);
        comment.setUserId(userId);
        comment.setContent(content);
        comment.setCt(DateUtils.getToday());
        return jdNewsCommentMapper.insert(comment);
    }
}
